package com.ruoyi.web.controller.memberCoupon;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.util.AbdulEmptyUtils;
import com.ruoyi.system.util.BaseResponse;
import com.ruoyi.system.util.StoreIdUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.StoredValueCardManagement;
import com.ruoyi.system.service.IStoredValueCardManagementService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 储值卡管理 Controller
 *
 * @author ruoyi
 * @date 2025-05-20
 */
@Api(tags = "储值卡管理")
@RestController
@RequestMapping("/system/storedValueCardManagement")
public class StoredValueCardManagementController extends BaseController {
    @Autowired
    private IStoredValueCardManagementService storedValueCardManagementService;

    @Autowired
    private StoreIdUtils storeIdUtils;

    /**
     * 通过卡名称和卡状态查询储值卡管理列表
     */
    @ApiOperation("通过卡名称和卡状态查询储值卡管理列表")
    @PreAuthorize("@ss.hasPermi('system:management:list')")
    @GetMapping("/page")
    public TableDataInfo page(@RequestParam(required = false) String cardName, @RequestParam(required = false) String status) {
        startPage();
        List<StoredValueCardManagement> list = storedValueCardManagementService.selectStoredValueCardManagementListByConditions(cardName, status);
        return getDataTable(list);
    }

    /**
     * 导出储值卡管理列表
     */
    @ApiOperation("导出储值卡管理列表")
    @PreAuthorize("@ss.hasPermi('system:management:export')")
    @Log(title = "储值卡管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestParam(required = false) String cardName, @RequestParam(required = false) String status) {
        List<StoredValueCardManagement> list = storedValueCardManagementService.selectStoredValueCardManagementListByConditions(cardName, status);
        ExcelUtil<StoredValueCardManagement> util = new ExcelUtil<StoredValueCardManagement>(StoredValueCardManagement.class);
        util.exportExcel(response, list, "储值卡管理数据");
    }

    /**
     * 通过id获取储值卡管理详细信息
     */
    @ApiOperation("通过id获取储值卡管理详细信息")
    @PreAuthorize("@ss.hasPermi('system:management:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(storedValueCardManagementService.selectStoredValueCardManagementById(id));
    }

    /**
     * 新增储值卡管理
     */
    @ApiOperation("新增储值卡管理")
    @PreAuthorize("@ss.hasPermi('system:management:add')")
    @Log(title = "储值卡管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StoredValueCardManagement storedValueCardManagement) {
        return toAjax(storedValueCardManagementService.insertStoredValueCardManagement(storedValueCardManagement));
    }

    /**
     * 修改储值卡管理
     */
    @ApiOperation("修改储值卡管理")
    @PreAuthorize("@ss.hasPermi('system:management:edit')")
    @Log(title = "储值卡管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StoredValueCardManagement storedValueCardManagement) {
        return toAjax(storedValueCardManagementService.updateStoredValueCardManagement(storedValueCardManagement));
    }

    /**
     * 通过id批量删除储值卡管理信息
     */
    @ApiOperation("通过id批量删除储值卡管理信息")
    @PreAuthorize("@ss.hasPermi('system:management:remove')")
    @Log(title = "储值卡管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(storedValueCardManagementService.deleteStoredValueCardManagementByIds(ids));
    }


    @ApiOperation("查询所有的列表")
    @GetMapping("/list")
    public BaseResponse<List<StoredValueCardManagement>> list(Integer cardType) {
        if (AbdulEmptyUtils.isEmpty(cardType)) {
            throw new RuntimeException("请选择储值卡类型");
        }
        LambdaQueryWrapper<StoredValueCardManagement> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StoredValueCardManagement::getCardType, cardType);
        wrapper.eq(StoredValueCardManagement::getStoreId, storeIdUtils.getStoreId());
        wrapper.eq(StoredValueCardManagement::getStatus, 2);
        return BaseResponse.success(storedValueCardManagementService.list(wrapper));
    }


}
